#!/bin/sh -e

. CI/travis/lib.sh

install_sphinx() {
	if [ "$CI_BUILD_SPHINX_DOCS" != "1" ] ; then
		return
	fi

	if ! is_python_at_least_ver "$PYTHON" "3.6" ; then
		echo_red "Python version is too old no python interpreter installed"
		return 1
	fi

	$PYTHON --version
	command -v $PYTHON
	$PYTHON -m pip install sphinx
	$PYTHON -m pip install sphinx-rtd-theme
}

handle_centos() {
	# needed for man2html and a few other popular tools
	yum search epel-release
	yum info epel-release
	yum -y install epel-release

	# FIXME: see about adding `libserialport-dev` from EPEL ; maybe libusb-1.0.0-devel...
	yum -y groupinstall 'Development Tools'
	yum -y install cmake libxml2-devel libusb1-devel libaio-devel \
		bzip2 gzip rpm rpm-build

	if [ "$(get_version | head -c 1)" = "7" ] ; then
		# install Cmake3, and make it the default
		yum -y install cmake3
		alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 \
			--slave /usr/local/bin/ctest ctest /usr/bin/ctest \
			--slave /usr/local/bin/cpack cpack /usr/bin/cpack \
			--slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake \
			--family cmake
		alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 \
			--slave /usr/local/bin/ctest ctest /usr/bin/ctest3 \
			--slave /usr/local/bin/cpack cpack /usr/bin/cpack3 \
			--slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 \
			--family cmake
	fi

	if is_centos_at_least_ver "8" ; then
		# On CentOS 8, avahi-devel & doxygen are in this repo; enable it
		yum -y install yum-utils
		yum config-manager --set-enabled powertools
		# On CentOS 6 & 7, have issues building or packaging doc
		yum -y install python3 doxygen man2html
		install_sphinx
	else
		# On CentOS 8, cdk-devel (Curses Development Kit) does not exist yet
		yum -y install ncurses-devel cdk-devel
	fi

	yum -y install avahi-devel
}

handle_generic_docker() {
	prepare_docker_image
}

handle_default() {
	sudo apt-get -qq update
	sudo DEBIAN_FRONTEND=noninteractive apt-get install -y cmake graphviz \
		libaio-dev libavahi-client-dev libserialport-dev \
		libavahi-common-dev libusb-1.0-0-dev libxml2-dev rpm tar \
		bzip2 gzip flex bison git libncurses5-dev libcdk5-dev \
		doxygen man2html python3 python3-pip python3-setuptools

	install_sphinx
}

handle_ubuntu() {
	handle_default
}

handle_debian() {
	handle_default
}

setup_build_type_env_vars

handle_${BUILD_TYPE}
